define(['jquery', 'bootstrap', 'moment', 'moment/locale/zh-cn', 'bootstrap-table', 'bootstrap-table-lang', 'bootstrap-table-export', 'bootstrap-table-commonsearch', 'bootstrap-table-template', 'bootstrap-table-jumpto'], function ($, undefined, Moment) {
    var Table = {
        list: {},
        // Bootstrap-table 基础配置
        defaults: {
            url: '',
            sidePagination: 'server',
            method: 'get', //请求方法
            toolbar: ".toolbar", //工具栏
            search: true, //是否启用快速搜索
            cache: false,
            commonSearch: true, //是否启用通用搜索
            searchFormVisible: false, //是否始终显示搜索表单
            titleForm: '', //为空则不显示标题，不定义默认显示：普通搜索
            idTable: 'commonTable',
            showExport: true,
            exportDataType: "all",
            exportTypes: ['json', 'xml', 'csv', 'txt', 'doc', 'excel'],
            exportOptions: {
               mso:{
                   // fileFormat:        'xlsx',
                    //修复导出数字不显示为科学计数法
                  onMsoNumberFormat: function (cell, row, col) {
                      return !isNaN($(cell).text())?'\\@':'';
                  }
               },
           },
            pageSize: 10,
            pageList: [10, 25, 50, 1000, 'All'],
            pagination: true,
            clickToSelect: true, //是否启用点击选中
            dblClickToEdit: false, //是否启用双击编辑
            singleSelect: false, //是否启用单选
            showRefresh: false,
            showJumpto: true,
            locale: 'zh-CN',
            showToggle: true,
            showColumns: true,
            pk: 'id',
            sortName: 'id',
            sortOrder: 'desc',
            paginationFirstText: __("First"),
            paginationPreText: __("Previous"),
            paginationNextText: __("Next"),
            paginationLastText: __("Last"),
            cardView: false, //卡片视图
            checkOnInit: true, //是否在初始化时判断
            escape: true, //是否对内容进行转义
            extend: {
                index_url: '',
                add_url: '',
                edit_url: '',
                del_url: '',
                import_url: '',
                multi_url: '',
                dragsort_url: 'ajax/weigh',
            }
        },
        // Bootstrap-table 列配置
        columnDefaults: {
            align: 'center',
            valign: 'middle',
        },
        config: {
            firsttd: 'tbody tr td:first-child:not(:has(div.card-views))',
            toolbar: '.toolbar',
            refreshbtn: '.btn-refresh',
            addbtn: '.btn-add',
            sendbtn: '.btn-send',
            delivergoodsbtn:'.btn-delivergoods',
            kefusendsbtn:'.btn-kefusend',
            fenleisbtn: '.btn-fenleis',
            tovoidbtn: '.btn-tovoid',
            returngoodsbtn: '.btn-returngoods',
            editbtn: '.btn-edit',
            statusbtn: '.btn-status',
            checkbtn: '.btn-check',
            generatebtn: '.btn-generate',
            msgbtn: '.btn-msg',
            excelbtn: '.btn-excel',
            generate_taxbtn: '.btn-generate_tax',
            generate_groupbtn: '.btn-generate_group',
            delbtn: '.btn-del',
            probtn: '.btn-pro',
            ministrialbtn: '.btn-ministrial',
            delsbtn: '.btn-dels',
            copysbtn: '.btn-copys',
            addsbtn: '.btn-adds',
            financebtn: '.btn-finance',
            importbtn: '.btn-import',
            downloadbtn: '.btn-download',
            stat: '.btn-stat',
            multibtn: '.btn-multi',
            disabledbtn: '.btn-disabled',
            editonebtn: '.btn-editone',
            copybtn: '.btn-copy',
            checkonebtn: '.btn-checkone',
            statusonebtn: '.btn-statusone',
            checksamplebtn: '.btn-checksample',
            dragsortfield: 'weigh',
        },
        button: {
          edit: {
            name: 'edit',
            icon: 'fa fa-pencil',
            title: __('Edit'),
            extend: 'data-toggle="tooltip"',
            classname: 'btn btn-xs btn-success btn-editone'
          },
          del: {
            name: 'del',
            icon: 'fa fa-trash',
            title: __('Del'),
            extend: 'data-toggle="tooltip"',
            classname: 'btn btn-xs btn-danger btn-delone'
          },
          copy:{
            name: 'copy',
            icon: 'fa fa-copy',
            title: __('复制产品'),
            extend: 'data-toggle="tooltip"',
            classname: 'btn btn-xs btn-success btn-copy',
          },
          dragsort: {
            name: 'dragsort',
            icon: 'fa fa-arrows',
            title: __('Drag to sort'),
            extend: 'data-toggle="tooltip"',
            classname: 'btn btn-xs btn-primary btn-dragsort'
          }
        },
        api: {
            init: function (defaults, columnDefaults, locales) {
                defaults = defaults ? defaults : {};
                columnDefaults = columnDefaults ? columnDefaults : {};
                locales = locales ? locales : {};
                // 如果是iOS设备则启用卡片视图
                if (navigator.userAgent.match(/(iPod|iPhone|iPad)/)) {
                    Table.defaults.cardView = true;
                }
                // 写入bootstrap-table默认配置
                $.extend(true, $.fn.bootstrapTable.defaults, Table.defaults, defaults);
                // 写入bootstrap-table column配置
                $.extend($.fn.bootstrapTable.columnDefaults, Table.columnDefaults, columnDefaults);
                // 写入bootstrap-table locale配置
                $.extend($.fn.bootstrapTable.locales[Table.defaults.locale], {
                    formatCommonSearch: function () {
                        return __('Common search');
                    },
                    formatCommonSubmitButton: function () {
                        return __('Submit');
                    },
                    formatCommonResetButton: function () {
                        return __('Reset');
                    },
                    formatCommonCloseButton: function () {
                        return __('Close');
                    },
                    formatCommonChoose: function () {
                        return __('Choose');
                    },
                    formatJumpto: function () {
                        return __('Go');
                    }
                }, locales);
            },
            // 绑定事件
            bindevent: function (table) {
                //Bootstrap-table的父元素,包含table,toolbar,pagnation
                var parenttable = table.closest('.bootstrap-table');
                //Bootstrap-table配置
                var options = table.bootstrapTable('getOptions');
                //Bootstrap操作区
                var toolbar = $(options.toolbar, parenttable);
                //当刷新表格时
                table.on('load-error.bs.table', function (status, res, e) {
                    if (e.status === 0) {
                        return;
                    }
                    Toastr.error(__('Unknown data format'));
                });
                //当刷新表格时
                table.on('refresh.bs.table', function (e, settings, data) {
                    $(Table.config.refreshbtn, toolbar).find(".fa").addClass("fa-spin");
                });
                if (options.dblClickToEdit) {
                    //当双击单元格时
                    table.on('dbl-click-row.bs.table', function (e, row, element, field) {
                        $(Table.config.editonebtn, element).trigger("click");
                    });
                }
                //当内容渲染完成后
                table.on('post-body.bs.table', function (e, settings, json, xhr) {
                    $(Table.config.refreshbtn, toolbar).find(".fa").removeClass("fa-spin");
                    $(Table.config.disabledbtn, toolbar).toggleClass('disabled', true);
                    if ($(Table.config.firsttd, table).find("input[type='checkbox'][data-index]").size() > 0) {
                        // 挺拽选择,需要重新绑定事件
                        require(['drag', 'drop'], function () {
                            $(Table.config.firsttd, table).drag("start", function (ev, dd) {
                                return $('<div class="selection" />').css('opacity', .65).appendTo(document.body);
                            }).drag(function (ev, dd) {
                                $(dd.proxy).css({
                                    top: Math.min(ev.pageY, dd.startY),
                                    left: Math.min(ev.pageX, dd.startX),
                                    height: Math.abs(ev.pageY - dd.startY),
                                    width: Math.abs(ev.pageX - dd.startX)
                                });
                            }).drag("end", function (ev, dd) {
                                $(dd.proxy).remove();
                            });
                            $(Table.config.firsttd, table).drop("start", function () {
                                Table.api.toggleattr(this);
                            }).drop(function () {
                                Table.api.toggleattr(this);
                            }).drop("end", function () {
                                Table.api.toggleattr(this);
                            });
                            $.drop({
                                multi: true
                            });
                        });
                    }
                });
                // 处理选中筛选框后按钮的状态统一变更
                table.on('check.bs.table uncheck.bs.table check-all.bs.table uncheck-all.bs.table', function () {
                    var ids = Table.api.selectedids(table);
                    $(Table.config.disabledbtn, toolbar).toggleClass('disabled', !ids.length);
                });
              // 绑定TAB事件 2020-05-21 10:02:31 杨柳
              $('.panel-heading [data-field] a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                var field = $(this).closest("[data-field]").data("field");
                var value = $(this).data("value");
                var object = $("[name='" + field + "']", table.closest(".bootstrap-table").find(".commonsearch-table"));
                if (object.prop('tagName') == "SELECT") {
                  $("option[value='" + value + "']", object).prop("selected", true);
                } else {
                  object.val(value);
                }
                table.bootstrapTable('refresh', {pageNumber: 1});
                return false;
              });
                // 刷新按钮事件
                $(toolbar).on('click', Table.config.refreshbtn, function () {
                    table.bootstrapTable('refresh');
                });
                // 添加按钮事件
                $(toolbar).on('click', Table.config.addbtn, function () {
                    var ids = Table.api.selectedids(table);
                    var url = options.extend.add_url;
                    if (url.indexOf("{ids}") !== -1) {
                        url = Table.api.replaceurl(url, {ids: ids.length > 0 ? ids.join(",") : 0}, table);
                    }
                    Fast.api.open(url, __('Add'), $(this).data() || {});
                });
                // 添加发货按钮事件
                $(toolbar).on('click', Table.config.delivergoodsbtn, function () {
                    var ids = Table.api.selectedids(table);
                    var url = options.extend.delivergoods_url;
                    if (url.indexOf("{ids}") !== -1) {
                        url = Table.api.replaceurl(url, {ids: ids.length > 0 ? ids.join(",") : 0}, table);
                    }
                    Fast.api.open(url, __('发货'), $(this).data() || {});
                });
                // 添加网购发货按钮事件
                $(toolbar).on('click', Table.config.kefusendsbtn, function () {
                    var ids = Table.api.selectedids(table);
                    var url = options.extend.kefusend_url;
                    if (url.indexOf("{ids}") !== -1) {
                        url = Table.api.replaceurl(url, {ids: ids.length > 0 ? ids.join(",") : 0}, table);
                    }
                    Fast.api.open(url, __('发货'), $(this).data() || {});
                });
                // 添加发货按钮事件
                $(toolbar).on('click', Table.config.sendbtn, function () {
                    var ids = Table.api.selectedids(table);
                    var url = options.extend.send_url;
                    if (url.indexOf("{ids}") !== -1) {
                        url = Table.api.replaceurl(url, {ids: ids.length > 0 ? ids.join(",") : 0}, table);
                    }
                    Fast.api.open(url, __('调货'), $(this).data() || {});
                });
                // 添加分类按钮事件
                $(toolbar).on('click', Table.config.fenleisbtn, function () {
                    var ids = Table.api.selectedids(table);
                    var url = options.extend.fenleis_url;
                    if (url.indexOf("{ids}") !== -1) {
                        url = Table.api.replaceurl(url, {ids: ids.length > 0 ? ids.join(",") : 0}, table);
                    }
                    Fast.api.open(url, __('产品分类调整'), $(this).data() || {});
                });
                // 添加作废按钮事件
                $(toolbar).on('click', Table.config.tovoidbtn, function () {
                    var ids = Table.api.selectedids(table);
                    var url = options.extend.tovoid_url;
                    if (url.indexOf("{ids}") !== -1) {
                        url = Table.api.replaceurl(url, {ids: ids.length > 0 ? ids.join(",") : 0}, table);
                    }
                    Fast.api.open(url, __('产品作废'), $(this).data() || {});
                });
                // 添加作废按钮事件
                $(toolbar).on('click', Table.config.returngoodsbtn, function () {
                    var ids = Table.api.selectedids(table);
                    var url = options.extend.returngoods_url;
                    if (url.indexOf("{ids}") !== -1) {
                        url = Table.api.replaceurl(url, {ids: ids.length > 0 ? ids.join(",") : 0}, table);
                    }
                    Fast.api.open(url, __('产品退货'), $(this).data() || {});
                });
                // 导入按钮事件
                if ($(Table.config.importbtn, toolbar).size() > 0) {
                    require(['upload'], function (Upload) {
                        Upload.api.plupload($(Table.config.importbtn, toolbar), function (data, ret) {
                            Fast.api.ajax({
                                url: options.extend.import_url,
                                data: {file: data.url},
                            }, function (data, ret) {
                                table.bootstrapTable('refresh');
                            });
                        });
                    });
                }
                //导出模板下载
                $(toolbar).on('click', Table.config.downloadbtn, function () {
                  var ids = Table.api.selectedids(table);
                  var url = options.extend.download_url;
                  if (url.indexOf("{ids}") !== -1) {
                    url = Table.api.replaceurl(url, {ids: ids.length > 0 ? ids.join(",") : 0}, table);
                  }
                  Fast.api.open(url, __('生成发货单模板下载'), $(this).data() || {});
                });
                //统计
                $(toolbar).on('click',Table.config.stat,function () {
                  var ids = Table.api.selectedids(table);
                  var url = options.extend.stat_url;
                  if (url.indexOf("{ids}") !== -1) {
                    url = Table.api.replaceurl(url, {ids: ids.length > 0 ? ids.join(",") : 0}, table);
                  }
                  Fast.api.open(url, __('数据统计'), $(this).data() || {});
                });
                // 批量编辑按钮事件
                $(toolbar).on('click', Table.config.editbtn, function () {
                    var that = this;
                    //循环弹出多个编辑框
                    $.each(table.bootstrapTable('getSelections'), function (index, row) {
                        var url = options.extend.edit_url;
                        row = $.extend({}, row ? row : {}, {ids: row[options.pk]});
                        var url = Table.api.replaceurl(url, row, table);
                        Fast.api.open(url, __('Edit'), $(that).data() || {});
                    });
                });
                // 生成数据
                $(toolbar).on('click', Table.config.generatebtn, function () {
                    var that = this;
            var url = options.extend.generate_url;
            Fast.api.open(url, '处理数据', $(that).data() || {});
                });
                // 批量发送短信
                $(toolbar).on('click', Table.config.msgbtn, function () {
                var that = this;
                var ids = Table.api.selectedids(table);
                var url = options.extend.msg_url+'/ids/'+ids.join(',');
                Fast.api.open(url, '短信发送', $(that).data() || {});
              });
                // 导入数据
                $(toolbar).on('click', Table.config.excelbtn, function () {
                    var that = this;
					var url = options.extend.excel_url;
					Fast.api.open(url, '处理数据', $(that).data() || {});
                });
                // 导入数据
                $(toolbar).on('click', Table.config.generate_taxbtn, function () {
                    var that = this;
                    var url = options.extend.generate_tax_url;
                    Fast.api.open(url, '处理数据', $(that).data() || {});
                });   // 导入数据
                $(toolbar).on('click', Table.config.generate_groupbtn, function () {
                    var that = this;
					var url = options.extend.generate_group_url;
					Fast.api.open(url, __('Generate_group'), $(that).data() || {});
                });
                // 批量审核按钮事件
                $(toolbar).on('click', Table.config.statusbtn, function () {
                    var that = this;
					var url = options.extend.status_url;
					var ids = Table.api.selectedids(table);
					url =  Table.api.replaceurl(url, {ids: ids}, table);
					Fast.api.open(url, __('SubStatus'), {area:["30%", "50%"]});
                });
                // 批量提交审核按钮事件
                $(toolbar).on('click', Table.config.checkbtn, function () {
                    var that = this;
					var url = options.extend.check_url;
					var ids = Table.api.selectedids(table);
					Layer.confirm(
                        __('Confirm submission of  %s selected item?', ids.length),
                        {icon: 3, title: __('Warning'), offset: 0, shadeClose: true},
                        function (index) {
                            Table.api.multi("check", ids, table, that);
                            Layer.close(index);
                        }
                    );
                });
                // 批量操作按钮事件
                $(toolbar).on('click', Table.config.multibtn, function () {
                    var ids = Table.api.selectedids(table);
                    Table.api.multi($(this).data("action"), ids, table, this);
                });
                // 批量删除按钮事件
                $(toolbar).on('click', Table.config.delbtn, function () {
                    var that = this;
                    var ids = Table.api.selectedids(table);
                    Layer.confirm(
                        __('Are you sure you want to delete the %s selected item?', ids.length),
                        {icon: 3, title: __('Warning'),  shadeClose: true},
                        function (index) {
                            Table.api.multi("del", ids, table, that);
                            Layer.close(index);
                        }
                    );
                });
                // 批量修改分类科目版本按钮事件
                $(toolbar).on('click', Table.config.probtn, function () {
                  var that = this;
                  var ids = Table.api.selectedids(table);
                  var url = options.extend.pro_url;
                  url = Table.api.replaceurl(url, {ids: ids.length > 0 ? ids.join(",") : 0}, table);
                  Fast.api.open(url, __('批量修改产品科目版本'), $(this).data() || {});
                });
              // 批量修改分类科目版本按钮事件
              $(toolbar).on('click', Table.config.ministrialbtn, function () {
                var that = this;
                var ids = Table.api.selectedids(table);
                var url = options.extend.ministrial_url;
                url = Table.api.replaceurl(url, {ids: ids.length > 0 ? ids.join(",") : 0}, table);
                Fast.api.open(url, __('批量修改码洋'), $(this).data() || {});
              });
                //批量替换
                $(toolbar).on('click', Table.config.copysbtn, function () {
                var that = this;
                var ids = Table.api.selectedids(table);
                layer.prompt({title: '名称 替换的内容', formType: 2}, function(pass, index){
                  layer.close(index);
                  layer.prompt({title: '名称 替换为', formType: 2}, function(text, index){
                    Layer.confirm(
                      '替换的内容：'+ pass +'<br>替换为：'+text,
                      {icon: 6, title: __('Warning'),  shadeClose: true},
                      function (index) {
                        Table.api.multi("copys", ids, table, {params: pass+","+text});
                        Layer.close(index);
                      }
                    );
                    Layer.close(index);

                  });
                });
              });
                //快捷添加
                $(toolbar).on('click', Table.config.addsbtn, function () {
                var that = this;
                var ids = Table.api.selectedids(table);
                layer.prompt({title: '请填写码洋', formType: 2}, function(price, index){
                  layer.close(index);
                  Layer.confirm(
                    '新增加的数据码洋为：'+ price +'<br>',
                    {icon: 6, title: __('Warning'),  shadeClose: true},
                    function (index) {
                      Table.api.multi("adds", ids, table, {params: price});
                      Layer.close(index);
                    }
                  );
                });
              });
                // 清楚数据按钮事件
                $(toolbar).on('click', Table.config.delsbtn, function () {
                  var url = options.extend.dels_url;
                  Fast.api.ajax({
                    url:url ,
                  }, function (data, ret) {
                    table.bootstrapTable('refresh');
                  });
                });

                // 批量审核通过
                $(toolbar).on('click', Table.config.financebtn, function () {
                  var that = this;
                  var ids = Table.api.selectedids(table);
                  layer.confirm(__('确定审核通过选择的 %s 项吗？', ids.length), {
                    btn: ['通过','不通过'] //按钮
                  }, function(index){
                    Table.api.multi("finance", ids, table, that);
                    Layer.close(index);
                  }, function(index){
                    layer.prompt({title: '输入不通过原因', formType: 2}, function(pass, index){
                      Table.api.multi("finance", ids, table, {params: pass});
                      layer.close(index);
                    });
                  });
              });
                 // 批量发送
                $(toolbar).on('click', Table.config.checksamplebtn, function () {
                    var that = this;
                    var url = options.extend.check_url;
                    var ids = Table.api.selectedids(table);
                    url =  Table.api.replaceurl(url, {ids: ids}, table);
                    Layer.confirm(
                        __('Are you sure you want to send a short message to the selected %s bar?', ids.length),
                        {icon: 3, title: __('Warning'),  shadeClose: true},
                        function (index) {
                            Layer.close(index);
                            Fast.api.open(url, __('Send'),$(this).data() || {});
                        }
                    );
                    
                  
                });
                // 拖拽排序
                require(['dragsort'], function () {
                    //绑定拖动排序
                    $("tbody", table).dragsort({
                        itemSelector: 'tr:visible',
                        dragSelector: "a.btn-dragsort",
                        dragEnd: function (a, b) {
                            var element = $("a.btn-dragsort", this);
                            var data = table.bootstrapTable('getData');
                            var current = data[parseInt($(this).data("index"))];
                            var options = table.bootstrapTable('getOptions');
                            //改变的值和改变的ID集合
                            var ids = $.map($("tbody tr:visible", table), function (tr) {
                                return data[parseInt($(tr).data("index"))][options.pk];
                            });
                            var changeid = current[options.pk];
                            var pid = typeof current.pid != 'undefined' ? current.pid : '';
                            var params = {
                                url: table.bootstrapTable('getOptions').extend.dragsort_url,
                                data: {
                                    ids: ids.join(','),
                                    changeid: changeid,
                                    pid: pid,
                                    field: Table.config.dragsortfield,
                                    orderway: options.sortOrder,
                                    table: options.extend.table
                                }
                            };
                            Fast.api.ajax(params, function (data, ret) {
                                var success = $(element).data("success") || $.noop;
                                if (typeof success === 'function') {
                                    if (false === success.call(element, data, ret)) {
                                        return false;
                                    }
                                }
                                table.bootstrapTable('refresh');
                            }, function () {
                                var error = $(element).data("error") || $.noop;
                                if (typeof error === 'function') {
                                    if (false === error.call(element, data, ret)) {
                                        return false;
                                    }
                                }
                                table.bootstrapTable('refresh');
                            });
                        },
                        placeHolderTemplate: ""
                    });
                });
                $(table).on("click", "input[data-id][name='checkbox']", function (e) {
                    var ids = $(this).data("id");
                    var row = Table.api.getrowbyid(table, ids);
                    table.trigger('check.bs.table', [row, this]);
                });
                $(table).on("click", "[data-id].btn-change", function (e) {
                    e.preventDefault();
                    Table.api.multi($(this).data("action") ? $(this).data("action") : '', [$(this).data("id")], table, this);
                });
                $(table).on("click", "[data-id].btn-edit", function (e) {
                    e.preventDefault();
                    var ids = $(this).data("id");
                    var row = Table.api.getrowbyid(table, ids);
                    row.ids = ids;
                    var url = Table.api.replaceurl(options.extend.edit_url, row, table);
                    Fast.api.open(url, __('Edit'), $(this).data() || {});
                });
                $(table).on("click", "[data-id].btn-del", function (e) {
                    e.preventDefault();
                    var id = $(this).data("id");
                    var that = this;
                    Layer.confirm(
                        __('Are you sure you want to delete this item?'),
                        {icon: 3, title: __('Warning'), shadeClose: true},
                        function (index) {
                            Table.api.multi("del", id, table, that);
                            Layer.close(index);
                        }
                    );
                });
                var id = table.attr("id");
                Table.list[id] = table;
                return table;
            },
            // 批量操作请求
            multi: function (action, ids, table, element) {
                var options = table.bootstrapTable('getOptions');
                var data = element ? $(element).data() : {};
                var ids = ($.isArray(ids) ? ids.join(",") : ids);
                if(typeof data.url !== "undefined"){
                  var url = data.url;
                }else{
                  switch (action) {
                     case 'del':
                      var url = options.extend.del_url;
                      break;
                    case 'become':
                      data = element ? element : {};
                      console.log(data)
                      var url = options.extend.become_url;
                      break;
                    case 'becomes':
                      var url = options.extend.becomes_url;
                      break;
                     case 'check':
                      var url = options.extend.check_url;
                    case 'finance':
                      data = element ? element : {};
                      var url = options.extend.finance_url;
                      //改变对象里面的值
                      break;
                    case 'can':
                      var url = options.extend.can_url;
                      break;
                    case 'byOut':
                      var url = options.extend.byOut_url;
                      break;
                    case 'copys':
                      data = element ? element : {};
                      var url = options.extend.copys_url;
                      break;
                    case 'adds':
                      data = element ? element : {};
                      var url = options.extend.adds_url;
                      break;
                     default:
                      var url = options.extend.multi_url;
                      break;
                  }
                }
                url = this.replaceurl(url, {ids: ids}, table);
                var params = typeof data.params !== "undefined" ? (typeof data.params == 'object' ? $.param(data.params) : data.params) : '';
                var options = {url: url, data: {action: action, ids: ids, params: params}};
                Fast.api.ajax(options, function (data, ret) {
                    var success = $(element).data("success") || $.noop;
                    if (typeof success === 'function') {
                        if (false === success.call(element, data, ret)) {
                            return false;
                        }
                    }
                    table.bootstrapTable('refresh');
                }, function (data, ret) {
                    var error = $(element).data("error") || $.noop;
                    if (typeof error === 'function') {
                        if (false === error.call(element, data, ret)) {
                            return false;
                        }
                    }
                });
            },
            // 单元格元素事件
            events: {
                operate: {
                    'click .btn-editone': function (e, value, row, index) {
                        e.stopPropagation();
                        e.preventDefault();
                        var table = $(this).closest('table');
                        var options = table.bootstrapTable('getOptions');
                        var ids = row[options.pk];
                        row = $.extend({}, row ? row : {}, {ids: ids});
                        var url = options.extend.edit_url;
                        Fast.api.open(Table.api.replaceurl(url, row, table), __('Edit'), $(this).data() || {});
                    },
                    'click .btn-copy': function (e, value, row, index) {
                        e.stopPropagation();
                        e.preventDefault();
                        var table = $(this).closest('table');
                        var options = table.bootstrapTable('getOptions');
                        var ids = row[options.pk];
                        row = $.extend({}, row ? row : {}, {ids: ids});
                        var url = options.extend.copy_url;
                        Fast.api.open(Table.api.replaceurl(url, row, table), __('复制数据'), $(this).data() || {});
                    },
                    'click .btn-coupon': function (e, value, row, index) {
                      e.stopPropagation();
                      e.preventDefault();
                      var table = $(this).closest('table');
                      var options = table.bootstrapTable('getOptions');
                      var ids = row[options.pk];
                      row = $.extend({}, row ? row : {}, {ids: ids});
                      var url = options.extend.coupon_url;
                      Fast.api.open(Table.api.replaceurl(url, row, table), __('发放优惠券'), $(this).data() || {});
                    },
                    'click .btn-couponlist': function (e, value, row, index) {
                      e.stopPropagation();
                      e.preventDefault();
                      var table = $(this).closest('table');
                      var options = table.bootstrapTable('getOptions');
                      var ids = row[options.pk];
                      row = $.extend({}, row ? row : {}, {ids: ids});
                      var url = options.extend.couponlist_url;
                      Fast.api.open(Table.api.replaceurl(url, row, table), __('查看列表'), $(this).data() || {});
                    },
                    'click .btn-booksone': function (e, value, row, index) {
                        e.stopPropagation();
                        e.preventDefault();
                        var table = $(this).closest('table');
                        var options = table.bootstrapTable('getOptions');
                        var ids = row[options.pk];
                        row = $.extend({}, row ? row : {}, {ids: ids});
                        var url = options.extend.books_url;
                        Fast.api.open(Table.api.replaceurl(url, row, table), __('Books.type 2'), $(this).data() || {});
                    },
                    'click .btn-checkone': function (e, value, row, index) {
                       e.stopPropagation();
                        e.preventDefault();
                        var that = this;
                        var top = $(that).offset().top - $(window).scrollTop();
                        var left = $(that).offset().left - $(window).scrollLeft() - 260;
                        if (top + 154 > $(window).height()) {
                            top = top - 154;
                        }
                        if ($(window).width() < 480) {
                            top = left = undefined;
                        }
                        Layer.confirm(
                            __('Confirm submission of  this item?'),
                            {icon: 3, title: __('Warning'), offset: [top, left], shadeClose: true},
                            function (index) {
                                var table = $(that).closest('table');
                                var options = table.bootstrapTable('getOptions');
                                Table.api.multi("check", row[options.pk], table, that);
                                Layer.close(index);
                            }
                        );
                    },
                    'click .btn-statusone': function (e, value, row, index) {
                        e.stopPropagation();
                        e.preventDefault();
                        var table = $(this).closest('table');
                        var options = table.bootstrapTable('getOptions');
                        var ids = row[options.pk];
                        row = $.extend({}, row ? row : {}, {ids: ids});
                        var url = options.extend.status_url;
                        Fast.api.open(Table.api.replaceurl(url, row, table), __('SubStatus'), {area:["30%", "50%"]});
                    },
                    'click .btn-delone': function (e, value, row, index) {
                        e.stopPropagation();
                        e.preventDefault();
                        var that = this;
                        var top = $(that).offset().top - $(window).scrollTop();
                        var left = $(that).offset().left - $(window).scrollLeft() - 260;
                        if (top + 154 > $(window).height()) {
                            top = top - 154;
                        }
                        if ($(window).width() < 480) {
                            top = left = undefined;
                        }
                        Layer.confirm(
                            __('Are you sure you want to delete this item?'),
                            {icon: 3, title: __('Warning'), offset: [top, left], shadeClose: true},
                            function (index) {
                                var table = $(that).closest('table');
                                var options = table.bootstrapTable('getOptions');
                                Table.api.multi("del", row[options.pk], table, that);
                                Layer.close(index);
                            }
                        );
                    },
                    'click .btn-finance': function (e, value, row, index) {
                      e.stopPropagation();
                      e.preventDefault();
                      var that = this;
                      var top = $(that).offset().top - $(window).scrollTop();
                      var left = $(that).offset().left - $(window).scrollLeft() - 260;
                      if (top + 154 > $(window).height()) {
                        top = top - 154;
                      }
                      if ($(window).width() < 480) {
                        top = left = undefined;
                      }
                      Layer.confirm(
                        __('确定审核通过此项吗？'),
                        {icon: 3, title: __('Warning'), offset: [top, left], shadeClose: true},
                        function (index) {
                          var table = $(that).closest('table');
                          var options = table.bootstrapTable('getOptions');
                          Table.api.multi("finance", row[options.pk], table, that);
                          Layer.close(index);
                        }
                      );
                    },
                    'click .btn-can': function (e, value, row, index) {
                      e.stopPropagation();
                      e.preventDefault();
                      var that = this;
                      var top = $(that).offset().top - $(window).scrollTop();
                      var left = $(that).offset().left - $(window).scrollLeft() - 260;
                      if (top + 154 > $(window).height()) {
                        top = top - 154;
                      }
                      if ($(window).width() < 480) {
                        top = left = undefined;
                      }
                      Layer.confirm(
                        __('确实要作废这条数据吗? 作废完不可恢复。'),
                        {icon: 3, title: __('Warning'), offset: [top, left], shadeClose: true},
                        function (index) {
                          var table = $(that).closest('table');
                          var options = table.bootstrapTable('getOptions');
                          Table.api.multi("can", row[options.pk], table, that);
                          Layer.close(index);
                        }
                      );
                    },
                    'click .btn-out': function (e, value, row, index) {
                      e.stopPropagation();
                      e.preventDefault();
                      var that = this;
                      var top = $(that).offset().top - $(window).scrollTop();
                      var left = $(that).offset().left - $(window).scrollLeft() - 260;
                      if (top + 154 > $(window).height()) {
                        top = top - 154;
                      }
                      if ($(window).width() < 480) {
                        top = left = undefined;
                      }
                      Layer.confirm(
                        __('确实要退款此订单吗? 退款完后,不可恢复。'),
                        {icon: 3, title: __('Warning'), offset: [top, left], shadeClose: true},
                        function (index) {
                          var table = $(that).closest('table');
                          var options = table.bootstrapTable('getOptions');
                          Table.api.multi("byOut", row[options.pk], table, that);
                          Layer.close(index);
                        }
                      );
                    },
                    'click .btn-stop': function (e, value, row, index) {
                      e.stopPropagation();
                      e.preventDefault();
                      var that = this;
                      var top = $(that).offset().top - $(window).scrollTop();
                      var left = $(that).offset().left - $(window).scrollLeft() - 260;
                      if (top + 154 > $(window).height()) {
                        top = top - 154;
                      }
                      if ($(window).width() < 480) {
                        top = left = undefined;
                      }
                      Layer.confirm(
                        __('确定要终止此任务吗？'),
                        {icon: 3, title: __('Warning'), offset: [top, left], shadeClose: true},
                        function (index) {
                          var table = $(that).closest('table');
                          var options = table.bootstrapTable('getOptions');
                          options.pk = 'id';
                          Table.api.multi("stop", row[options.pk], table, that);
                          Layer.close(index);
                        }
                      );
                    },
                    'click .btn-run-amor': function (e, value, row, index) {
                      e.stopPropagation();
                      e.preventDefault();
                      var table = $(this).closest('table');
                      var options = table.bootstrapTable('getOptions');
                      options.pk = 'from_id';
                      var ids = row[options.pk];
                      row = $.extend({}, row ? row : {}, {ids: ids});
                      var url = options.extend.run_url+'/wf_type/'+row['from_table']+'/wf_title/'+row['flow_name'];
                      Fast.api.open(Table.api.replaceurl(url, row, table), __('代审'), $(this).data() || {});
                    },
                    'click .btn-run-assistant': function (e, value, row, index) {
                      e.stopPropagation();
                      e.preventDefault();
                      var table = $(this).closest('table');
                      var options = table.bootstrapTable('getOptions');
                      var ids = row[options.pk];
                      row = $.extend({}, row ? row : {}, {ids: ids});
                      var url = options.extend.send_url+'/chatid/'+row['chatid'];
                      Fast.api.open(Table.api.replaceurl(url, row, table), __('发送'), $(this).data() || {});
                    },
                    'click .btn-checksample': function (e, value, row, index) {
                        e.stopPropagation();
                        e.preventDefault();
                        var table = $(this).closest('table');
                        var options = table.bootstrapTable('getOptions');
                        var ids = row[options.pk];
                        row = $.extend({}, row ? row : {}, {ids: ids});
                        var url = options.extend.check_url;
                        var that = this;
                        var top = $(that).offset().top - $(window).scrollTop();
                        var left = $(that).offset().left - $(window).scrollLeft() - 260;
                        if (top + 154 > $(window).height()) {
                            top = top - 154;
                        }
                        if ($(window).width() < 480) {
                            top = left = undefined;
                        }
                        Layer.confirm(
                            __('Are you sure you want to send a short message?'),
                            {icon: 3, title: __('Warning'), offset: [top, left], shadeClose: true},
                            function (index) {
                                 Layer.close(index);
                                Fast.api.open(Table.api.replaceurl(url, row, table), __('Send'), $(this).data() || {});
                               
                            }
                        );

                    },
                }
            },
            // 单元格数据格式化
            formatter: {
                icon: function (value, row, index) {
                    if (!value)
                        return '';
                    value = value.indexOf(" ") > -1 ? value : "fa fa-" + value;
                    //渲染fontawesome图标
                    return '<i class="' + value + '"></i> ' + value;
                },
                image: function (value, row, index) {
                    value = value ? value : '/assets/img/blank.gif';
                    var classname = typeof this.classname !== 'undefined' ? this.classname : 'img-sm img-center';
                    return '<a href="' + Fast.api.cdnurl(value) + '" target="_blank"><img class="' + classname + '" src="' + Fast.api.cdnurl(value) + '" /></a>';
                },
                images: function (value, row, index) {
                    value = value === null ? '' : value.toString();
                    var classname = typeof this.classname !== 'undefined' ? this.classname : 'img-sm img-center';
                    var arr = value.split(',');
                    var html = [];
                    $.each(arr, function (i, value) {
                        value = value ? value : '/assets/img/blank.gif';
                        html.push('<a href="' + Fast.api.cdnurl(value) + '" target="_blank"><img class="' + classname + '" src="' + Fast.api.cdnurl(value) + '" /></a>');
                    });
                    return html.join(' ');
                },
                status: function (value, row, index) {
                    var custom = {normal: 'success', hidden: 'gray', deleted: 'danger', locked: 'info'};
                    if (typeof this.custom !== 'undefined') {
                        custom = $.extend(custom, this.custom);
                    }
                    this.custom = custom;
                    this.icon = 'fa fa-circle';
                    return Table.api.formatter.normal.call(this, value, row, index);
                },
                normal: function (value, row, index) {
                    var colorArr = ["primary", "success", "danger", "warning", "info", "gray", "red", "yellow", "aqua", "blue", "navy", "teal", "olive", "lime", "fuchsia", "purple", "maroon"];
                    var custom = {};
                    if (typeof this.custom !== 'undefined') {
                        custom = $.extend(custom, this.custom);
                    }
                    var keys = typeof this.searchList === 'object' ? Object.keys(this.searchList) : [];
                    value = value === null ? '' : value.toString();
                    var index = keys.indexOf(value);
                   // value = value === null ? '' : value.toString();
                    var color = value && typeof custom[value] !== 'undefined' ? custom[value] : null;
                    var display = index > -1 ? this.searchList[value] : null;
                    var icon = typeof this.icon !== 'undefined' ? this.icon : null;
                    if (!color) {
                        color = index > -1 && typeof colorArr[index] !== 'undefined' ? colorArr[index] : 'primary';
                    }
                    if (!display) {
                        display = __(value.charAt(0).toUpperCase() + value.slice(1));
                    }
                    var html = '<span class="text-' + color + '">' + (icon ? '<i class="' + icon + '"></i> ' : '') + display + '</span>';
                    if (this.operate != false) {
                        html = '<a href="javascript:;" class="searchit" data-toggle="tooltip" title="' + __('Click to search %s', display) + '" data-field="' + this.field + '" data-value="' + value + '">' + html + '</a>';
                    }
                    return html;
                },
                toggle: function (value, row, index) {
                    var color = typeof this.color !== 'undefined' ? this.color : 'success';
                    var yes = typeof this.yes !== 'undefined' ? this.yes : 1;
                    var no = typeof this.no !== 'undefined' ? this.no : 0;
                    return "<a href='javascript:;' data-toggle='tooltip' title='" + __('Click to toggle') + "' class='btn-change' data-id='"
                        + row.id + "' data-params='" + this.field + "=" + (value ? no : yes) + "'><i class='fa fa-toggle-on " + (value == yes ? 'text-' + color : 'fa-flip-horizontal text-gray') + " fa-2x'></i></a>";
                }
                ,
                url: function (value, row, index) {
                    return '<div class="input-group input-group-sm" style="width:250px;margin:0 auto;"><input type="text" class="form-control input-sm" value="' + value + '"><span class="input-group-btn input-group-sm"><a href="' + value + '" target="_blank" class="btn btn-default btn-sm"><i class="fa fa-link"></i></a></span></div>';
                },
                search: function (value, row, index) {
                    return '<a href="javascript:;" class="searchit" data-toggle="tooltip" title="' + __('Click to search %s', value) + '" data-field="' + this.field + '" data-value="' + value + '">' + value + '</a>';
                },
                addtabs: function (value, row, index) {
                    var url = Table.api.replaceurl(this.url, row, this.table);
                    var title = this.atitle ? this.atitle : __("Search %s", value);
                    return '<a href="' + Fast.api.fixurl(url) + '" class="addtabsit" data-value="' + value + '" title="' + title + '">' + value + '</a>';
                },
                dialog: function (value, row, index) {
                    var url = Table.api.replaceurl(this.url, row, this.table);
                    var title = this.atitle ? this.atitle : __("View %s", value);
                    return '<a href="' + Fast.api.fixurl(url) + '" class="dialogit" data-value="' + value + '" title="' + title + '">' + value + '</a>';
                },
                flag: function (value, row, index) {
                    var that = this;
                    value = value === null ? '' : value.toString();
                    var colorArr = {index: 'success', hot: 'warning', recommend: 'danger', 'new': 'info'};
                    //如果字段列有定义custom
                    if (typeof this.custom !== 'undefined') {
                        colorArr = $.extend(colorArr, this.custom);
                    }
                    var field = this.field;
                    if (typeof this.customField !== 'undefined' && typeof row[this.customField] !== 'undefined') {
                        value = row[this.customField];
                        field = this.customField;
                    }

                    //渲染Flag
                    var html = [];
                    var arr = value.split(',');
                    $.each(arr, function (i, value) {
                        value = value === null ? '' : value.toString();
                        if (value == '')
                            return true;
                        var color = value && typeof colorArr[value] !== 'undefined' ? colorArr[value] : 'primary';
                        var display = typeof that.searchList !== 'undefined' && typeof that.searchList[value] !== 'undefined' ? that.searchList[value] : __(value.charAt(0).toUpperCase() + value.slice(1));
                        html.push('<a href="javascript:;" class="searchit" data-toggle="tooltip" title="' + __('Click to search %s', display) + '" data-field="' + field + '" data-value="' + value + '"><span class="label label-' + color + '">' + display + '</span></a>');
                    });
                    return html.join(' ');
                },
                label: function (value, row, index) {
                    return Table.api.formatter.flag.call(this, value, row, index);
                },
                datetime: function (value, row, index) {
                    var datetimeFormat = typeof this.datetimeFormat === 'undefined' ? 'YYYY-MM-DD HH:mm:ss' : this.datetimeFormat;
                    if (isNaN(value)) {
                        return value ? Moment(value).format(datetimeFormat) : __('None');
                    } else {
                        return value ? Moment(parseInt(value) * 1000).format(datetimeFormat) : __('None');
                    }
                },
                operate: function (value, row, index) {
                  var table = this.table;
                  // 操作配置
                  var options = table ? table.bootstrapTable('getOptions') : {};
                  // 默认按钮组
                  var buttons = $.extend([], this.buttons || []);
                  // 所有按钮名称
                  var names = [];
                  buttons.forEach(function (item) {
                    names.push(item.name);
                  });
                  if (options.extend.dragsort_url !== '' && names.indexOf('dragsort') === -1) {
                    buttons.push(Table.button.dragsort);
                  }
                  if (options.extend.edit_url !== '' && names.indexOf('edit') === -1) {
                    Table.button.edit.url = options.extend.edit_url;
                    buttons.push(Table.button.edit);
                  }
                  if (options.extend.del_url !== '' && names.indexOf('del') === -1) {
                    buttons.push(Table.button.del);
                  }
                  return Table.api.buttonlink(this, buttons, value, row, index, 'operate');
                },
                buttons: function (value, row, index) {
                    // 默认按钮组
                    var buttons = $.extend([], this.buttons || []);
                    return Table.api.buttonlink(this, buttons, value, row, index, 'buttons');
                }
            },
            buttonlink: function (column, buttons, value, row, index, type) {
              var table = column.table;
              type = typeof type === 'undefined' ? 'buttons' : type;
              var options = table ? table.bootstrapTable('getOptions') : {};
              var html = [];
              var hidden, visible, disable, url, classname, icon, text, title, refresh, confirm, extend,
                dropdown, link;
              var fieldIndex = column.fieldIndex;
              var dropdowns = {};

              $.each(buttons, function (i, j) {
                if (type === 'operate') {
                  if (j.name === 'dragsort' && typeof row[Table.config.dragsortfield] === 'undefined') {
                    return true;
                  }
                  if (['add', 'edit', 'del', 'multi', 'dragsort'].indexOf(j.name) > -1 && !options.extend[j.name + "_url"]) {
                    return true;
                  }
                }
                var attr = table.data(type + "-" + j.name);
                if (typeof attr === 'undefined' || attr) {
                  hidden = typeof j.hidden === 'function' ? j.hidden.call(table, row, j) : (typeof j.hidden !== 'undefined' ? j.hidden : false);
                  if (hidden) {
                    return true;
                  }
                  visible = typeof j.visible === 'function' ? j.visible.call(table, row, j) : (typeof j.visible !== 'undefined' ? j.visible : true);
                  if (!visible) {
                    return true;
                  }
                  dropdown = j.dropdown ? j.dropdown : '';
                  url = j.url ? j.url : '';
                  url = typeof url === 'function' ? url.call(table, row, j) : (url ? Fast.api.fixurl(Table.api.replaceurl(url, row, table)) : 'javascript:;');
                  classname = j.classname ? j.classname : 'btn-primary btn-' + name + 'one';
                  icon = j.icon ? j.icon : '';
                  text = typeof j.text === 'function' ? j.text.call(table, row, j) : j.text ? j.text : '';
                  title = typeof j.title === 'function' ? j.title.call(table, row, j) : j.title ? j.title : text;
                  refresh = j.refresh ? 'data-refresh="' + j.refresh + '"' : '';
                  confirm = typeof j.confirm === 'function' ? j.confirm.call(table, row, j) : (typeof j.confirm !== 'undefined' ? j.confirm : false);
                  confirm = confirm ? 'data-confirm="' + confirm + '"' : '';
                  extend = j.extend ? j.extend : '';
                  disable = typeof j.disable === 'function' ? j.disable.call(table, row, j) : (typeof j.disable !== 'undefined' ? j.disable : false);
                  if (disable) {
                    classname = classname + ' disabled';
                  }
                  link = '<a href="' + url + '" class="' + classname + '" ' + (confirm ? confirm + ' ' : '') + (refresh ? refresh + ' ' : '') + extend + ' title="' + title + '" data-table-id="' + (table ? table.attr("id") : '') + '" data-field-index="' + fieldIndex + '" data-row-index="' + index + '" data-button-index="' + i + '"><i class="' + icon + '"></i>' + (text ? ' ' + text : '') + '</a>';
                  if (dropdown) {
                    if (typeof dropdowns[dropdown] == 'undefined') {
                      dropdowns[dropdown] = [];
                    }
                    dropdowns[dropdown].push(link);
                  } else {
                    html.push(link);
                  }
                }
              });
              if (!$.isEmptyObject(dropdowns)) {
                var dropdownHtml = [];
                $.each(dropdowns, function (i, j) {
                  dropdownHtml.push('<div class="btn-group"><button type="button" class="btn btn-primary dropdown-toggle btn-xs" data-toggle="dropdown">' + i + '</button><button type="button" class="btn btn-primary dropdown-toggle btn-xs" data-toggle="dropdown"><span class="caret"></span></button><ul class="dropdown-menu pull-right"><li>' + j.join('</li><li>') + '</li></ul></div>');
                });
                html.unshift(dropdownHtml);
              }
              return html.join(' ');
            },
            //替换URL中的数据
            replaceurl: function (url, row, table) {
                var options = table ? table.bootstrapTable('getOptions') : null;
                var ids = options ? row[options.pk] : 0;
                row.ids = ids ? ids : (typeof row.ids !== 'undefined' ? row.ids : 0);
                //自动添加ids参数
                url = !url.match(/\{ids\}/i) ? url + (url.match(/(\?|&)+/) ? "&ids=" : "/ids/") + '{ids}' : url;

                url = url.replace(/\{(.*?)\}/gi, function (matched) {
                    matched = matched.substring(1, matched.length - 1);
                    if (matched.indexOf(".") !== -1) {
                        var temp = row;
                        var arr = matched.split(/\./);
                        for (var i = 0; i < arr.length; i++) {
                            if (typeof temp[arr[i]] !== 'undefined') {
                                temp = temp[arr[i]];
                            }
                        }
                        return typeof temp === 'object' ? '' : temp;
                    }
                    return row[matched];
                });
                return url;
            },
            // 获取选中的条目ID集合
            selectedids: function (table) {
                var options = table.bootstrapTable('getOptions');
                if (options.templateView) {
                    return $.map($("input[data-id][name='checkbox']:checked"), function (dom) {
                        return $(dom).data("id");
                    });
                } else {
                    return $.map(table.bootstrapTable('getSelections'), function (row) {
                        return row[options.pk];
                    });
                }
            },
            // 切换复选框状态
            toggleattr: function (table) {
                $("input[type='checkbox']", table).trigger('click');
            },
            // 根据行索引获取行数据
            getrowdata: function (table, index) {
                index = parseInt(index);
                var data = table.bootstrapTable('getData');
                return typeof data[index] !== 'undefined' ? data[index] : null;
            },
            // 根据行索引获取行数据
            getrowbyindex: function (table, index) {
                return Table.api.getrowdata(table, index);
            },
            // 根据主键ID获取行数据
            getrowbyid: function (table, id) {
                var row = {};
                var options = table.bootstrapTable("getOptions");
                $.each(table.bootstrapTable('getData'), function (i, j) {
                    if (j[options.pk] == id) {
                        row = j;
                        return false;
                    }
                });
                return row;
            }
        },
    };
    return Table;
});
